<?php

namespace app\api\controller;


use app\common\model\pachong\PachongXiaohua;
use app\common\model\RedisService;

class XiaohuaController extends ApiController
{

    public function index()
    {
        $id = $this->get['id'] ?? 1;

        // 查询当前和查询下一个
        $where[] = ['id', '=', $id];
        PachongXiaohua::where($where)->inc('num')->update();
        $PachongXiaohua = PachongXiaohua::where($where)->findOrEmpty()->toArray();
        if(empty($PachongXiaohua)){
            $PachongXiaohua = PachongXiaohua::limit(1)->select()->toArray()[0];
            $id = $PachongXiaohua['id'];
        }

        $nextWhere[] = ['id', '>', $id];
        $nextPachongXiaohua = PachongXiaohua::where($nextWhere)->order('id asc')->limit(1)->findOrEmpty()->toArray();
        // 查询上一个
        $beforeWhere[] = ['id', '<', $id];
        $beforePachongXiaohua = PachongXiaohua::where($beforeWhere)->order('id desc')->limit(1)->findOrEmpty()->toArray();

        $rs = array(
            'now' => $PachongXiaohua ?? array(),
            'before' => $beforePachongXiaohua ?? array(),
            'after' => $nextPachongXiaohua ?? array()
        );
        $this->setData($rs);
        $this->sendJson();
    }
}
